<?php
/**
 * Created by PhpStorm.
 * User: weichen
 * Date: 2017/5/7
 * Time: 21:30
 */

namespace AppApi\Controller;


use Common\Model\MemberModel;
use Tea\Model\TeaModel;
use Tea\Model\TeaWorksModel;

class TeaController extends ApiBaseController
{
    private $tea_model;
    private $member_model;
    private $tea_workds_model;

    public function __construct()
    {
        parent::__construct();
        $this->member_model = new MemberModel();
        $this->tea_model = new TeaModel();
        $this->tea_workds_model = new TeaWorksModel();
    }

    /**
     * 获取批次列表
     */
    public function getList() {
        $mid = I('post.mid');

        $page = I('post.page');
        $perpage = I('post.perpage');
        //页码
        $page = isset($page) && intval($page)>1 ? $page : '1';
        //每页显示数量
        $perpage = isset($perpage) && intval($perpage)>1 ? $perpage : '10';
        $count = $this->tea_model->where(['mid' => $mid])->count();

        $lists = $this->tea_model->where(['mid' => $mid])->field('id, number_sn, DATE_FORMAT(create_time, "%Y-%m-%d ") as create_time, status')->select();

        $totalpage = $count/$perpage;
        $totalpage = floor($totalpage);
        if($count % $perpage) {
            $totalpage += 1;
        }
        $row =[];
        foreach ($lists as $k => $v) {
            $row[] = [
                'id' => $v['id'],
                'number_sn' => $v['number_sn'],
                'create_time' => $v['create_time'],
                'status' => $this->tea_model->statusToString($v['status']),
            ];
        }
        $data['totalpage'] = $totalpage;
        $data['lists'] = $row;
        exit($this->returnApiSuccess($data));

    }

    public function getNextNumber() {
        $number_sn = $this->tea_model->order('id desc')->getField('number_sn');
        if($number_sn) {
            $number_sn += 1;
        } else {
            $number_sn = date('Ymd').'01';
        }
        exit($this->returnApiSuccess(['number_sn' => (string)$number_sn]));
//        dump($number_sn+1);
    }
    /**
     * 添加一个批次
     */
    public function addOne() {
        $mid = I('post.mid');
        $number_sn = I('post.number_sn');
        $create_time = I('post.create_time');
        $this->checkparam([$mid, $number_sn, $create_time]);

        $data = [
            'mid' => $mid,
            'create_time' => $create_time.' 00:00:00',
            'number_sn' => $number_sn,
        ];
        if($this->tea_model->add($data) == false) {
            exit($this->returnApiError(ApiBaseController::FATAL_ERROR, '失败'));
        } else {
            exit($this->returnApiSuccess());
        }
    }

    public function addTask() {
        $mid = I('post.mid');
        $type = I('post.type');
        $number_sn = I('post.number_sn');

        $this->checkparam([$mid, $type, $number_sn]);
        $data = [
            'create_time' => date('Y-m-d H:i:s'),
            'number_sn' => $number_sn,
            'type' => $type,
            'mid' => $mid,
        ];
        if($this->tea_workds_model->add($data) == false) {
            exit($this->returnApiError(ApiBaseController::FATAL_ERROR, '失败'));
        } else {
            exit($this->returnApiSuccess());
        }
    }
}